import { getFileOption, getQueryValueFromUrl } from './utils'
import { FILE_TYPE } from './constants'

export default {
  inject: {
    provideDisabled: {
      type: Boolean,
      default: false,
    }
  },
  props: {
    file: {
      type: null,
      default: null,
    },
    onDownload: {
      type: Function,
      default: new Function(),
    },
    onRemove: {
      type: Function,
      default: new Function(),
    },
    onReview: {
      type: Function,
      default: new Function(),
    },
  },
  data() {
    return {
      fileUrl: '',
      fileName: '',
    }
  },
  computed: {
    option() {
      const opt = getFileOption(this.fileName || this.fileUrl) || {}
      return {
        ...opt,
        imageUrl: opt.type === FILE_TYPE.IMAGE ? this.fileUrl : null,
      }
    },
  },
  created() {
    const { file } = this
    const type = Object.prototype.toString.call(file)
    if (type === '[object Object]') {
      this.fileUrl = file.url || file.fileUrl
      this.fileName = file.name || file.fileName
    } else if (type === '[object String]') {
      this.fileUrl = file
      const name = getQueryValueFromUrl(this.fileUrl, 'name')
      const fileName = getQueryValueFromUrl(this.fileUrl, 'fileName')
      this.fileName = fileName || name
    }
  }
}
